<template>
	<view>
		<view class="inp">
			<input type="text" v-model="delivery.name" placeholder="请输入收件人姓名" />
			<input type="text" v-model="delivery.phone" placeholder="请输入联系电话" />
			<input type="text" v-model='delivery.address' placeholder="请输入详细地址xx省(市)xx市(区)..." />
		</view>
		<view @click="onSubmit" class="onSubmit">
			<text>保存地址</text>
		</view>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import userApi from '../../../api/user.js'
	export default {
		data() {
			return {
				delivery: {
					name: '',
					phone: "",
					address: ""
				},
				messageText: '',
				msgType: "",
				_id: ''
			}
		},
		mounted() {
			this._id = this.$store.getters['user/get_id']
		},
		methods: {
			onSubmit() {
				let _this = this
				let state = true;
				let {
					name,
					phone,
					address
				} = this.delivery
				if (name == '' || phone == "" || address == "") {
					this.messageText = '请输入完整信息'
					this.msgType = "error"
					this.$refs.message.open()
				} else {
					userApi.update_addres({
						_id: this._id,
						delivery: this.delivery,
						cb: (data) => {
							userApi.find_by_id({
								_id: this._id,
								cb: (data) => {
									console.log(data)
									_this.$store.commit('user/set_delivery', data[0].delivery);
									console.log(_this.$store.getters['user/get_delivery'])
									_this.messageText = '添加地址成功'
									_this.msgType = "success"
									_this.$refs.message.open()
									uni.navigateBack({
										success: () => {
										uni.navigateBack({
											
										})
										}
									})
								}
							}, )

						}
					})
				}
				console.log(this.delivery)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #ebebeb;
	}

	.inp {
		input {
			border-bottom: 1rpx solid #E5E5E5;
			height: 80rpx;
			font-size: 34rpx;
			line-height: 80rpx;
			padding: 10rpx 30rpx;
		}
	}

	.onSubmit {
		position: absolute;
		bottom: 0;
		width: 100%;
		text-align: center;
		background-color: #78cdd1;
		height: 80rpx;
		line-height: 60rpx;
		padding-bottom: 20rpx;

		text {
			font-size: 40rpx;
			color: #FFFFFF;
			font-weight: bold;
		}
	}
</style>
